package com.m7788.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.m7788.dao.UploadProDataDaoImpl;
import com.m7788.po.UploadProData;
import com.m7788.screen.R;
import com.tool.custom.progressbar.CProgressBar;


public class ImageProAdapter extends BaseAdapter {
	private List<UploadProData> list;
	private LayoutInflater inflater;
	private Context context;
	private OnAdapterChangeListener listener;
	public ImageProAdapter(Context context, List<UploadProData> items) {
		this.list = items;
		this.context = context;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);		
	}
	
	public void setOnAdapterChangeListener(OnAdapterChangeListener listener){    
		this.listener = listener;
    }
	
	/**
	 * 获取数据集长度
	 */
	@Override
	public int getCount() {
		return list.size();
	}

	/**
	 * 根据位置获取当前数据
	 */
	@Override
	public Object getItem(int position) {
		return list.get(position);
	}
	
	/**
	 * 获取位置
	 */
	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;	
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.adapter_listview_image, null);
			holder = new ViewHolder();
			//获取文本控件
			holder.date =  (TextView)convertView.findViewById(R.id.ali_tv_date);
			holder.fail =  (TextView)convertView.findViewById(R.id.ali_tv_fail);
			holder.name = (TextView)convertView.findViewById(R.id.ali_tv_name);
			holder.name.setTag(position);
			//获取按钮
			holder.cancel = (Button)convertView.findViewById(R.id.ali_btn_close);
			//添加按钮事件
			holder.cancel.setOnClickListener(new MyOnClickListener(holder){
				@Override
				public void onClick(View v, ViewHolder holder) {
					if(list.size()>0){
						int p = (Integer)holder.name.getTag();
//						list.remove(p);
//						removeData(p);
						if(listener!=null){
							listener.listRemove(p);
						}
					}
				}});
			holder.cpb =  (CProgressBar)convertView.findViewById(R.id.ali_cpb);
			convertView.setTag(holder);
			
		}else{
			holder = (ViewHolder)convertView.getTag();
			holder.name.setTag(position);
		}
		
		
		
		UploadProData info = list.get(position);
		 holder.name.setText(info.getType()+"到："+info.getFileName());
		 holder.date.setText(""+info.getDate());
		 holder.fail.setText("失败图片数："+info.getFailNum());
		 holder.cpb.setMax(info.getTotal());
		 holder.cpb.setProgress(info.getCur(), info.getCur()+"/"+info.getTotal());
		 return convertView;
	}
	
	
	
	/**
	 * 
	 * 方法名：addItem 
	 * 功能：新增一项
	 * 参数：
	 * @param info
	 * 创建人：huanghsh  
	 * 创建时间：2012-6-8
	 */
	public void addItem(UploadProData info){
		list.add(info);
	}
	/**
	 * 
	 * 方法名：getAllList 
	 * 功能：获取采集的所有数据
	 * 参数：
	 * @return
	 * 创建人：huanghsh  
	 * 创建时间：2012-6-8
	 */
	public List<UploadProData> getAllList(){
		return list;
	}
	
	
	/**
	 * 
	 * 方法名：modifyListNumber 
	 * 功能：修改号码
	 * 参数：
	 * @param price
	 * @param p
	 * 创建人：huanghsh  
	 * 创建时间：2012-6-8
	 */
	public void removeData(int p){
		if(p>=list.size()) return;
		UploadProData info = list.get(p);
		UploadProDataDaoImpl daoImpl = new UploadProDataDaoImpl(context);
//		if(info.getFlag()==0){
//			info.setFlag(1);
//			daoImpl.update(info);
//		}else{
			daoImpl.delete(info.getId());
			list.remove(p);
			this.notifyDataSetChanged();
//		}
		
		

	}
	
	 private final class ViewHolder{         
	    	private Button cancel;   
	    	private TextView name;
	    	private TextView date;
	    	private TextView fail;
	    	private CProgressBar cpb;
	 }
	 
	 private abstract class MyOnClickListener implements View.OnClickListener{
		 
		 private ViewHolder mViewHolder;
		 public MyOnClickListener(ViewHolder viewHolder) {
				mViewHolder = viewHolder;
		 }
		@Override
		public void onClick(View v) {
			onClick(v,mViewHolder);
			
		}
		public abstract void onClick(View v,ViewHolder viewHolder); 
		 
	 }
	 public interface OnAdapterChangeListener {
			/**
			 * 
			 * 方法名：listRemove 
			 * 功能：减少项
			 * 参数：
			 * @param currentItem
			 * 创建人：huanghsh  
			 * 创建时间：2012-6-8
			 */
			public void listRemove(int currentItem);  
	 }
}
